package com.msec.permission.demo;

import android.Manifest;
import android.app.Application;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PermissionDemoApplication extends Application {

    private Application application;
    private static final String TAG = "permission_demo_logger";
    @Override
    public void onCreate() {
        super.onCreate();
        this.application = this;
        ActivityLifeCycleHandler.INSTANCE().register(this);

        /**
         * (1) UI还未初始化时请求
         * */
        RequestHelper.requestPermission(new String[]{ Manifest.permission.READ_PHONE_STATE}, new PermissionCallBack(null){

            @Override
            public void onGranted() {
                super.onGranted();
                TelephonyManager telephonyManager = (TelephonyManager) application.getSystemService(Context.TELEPHONY_SERVICE);
                if (telephonyManager != null) {
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                        boolean canChangeDtmfToneLength = telephonyManager.canChangeDtmfToneLength();
                        Log.i(TAG,"onRequestPermissionsResult granted:(1)" + canChangeDtmfToneLength);
                    }
                }
            }
        });
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        ActivityLifeCycleHandler.INSTANCE().remove(this);
    }
}
